package com.blog.attach.controller;


import com.blog.attach.service.UserCollectionService;
import com.blog.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 苏离
 * @since 2022-01-15
 */
@RestController
@CrossOrigin("*")
@RequestMapping("/attach/userCollection")
public class UserCollectionController {
    @Autowired
    UserCollectionService collectionService;

    /**
     * 传用户id和文章id查数据库检验该用户是否已经收藏这篇文章
     * @param userId 用户id
     * @param articleId 文章id
     * @return 收藏结果
     */
    @GetMapping("/isCollections/{userId}/{articleId}")
    public ResponseResult isCollections(@PathVariable String userId,
                                        @PathVariable Integer articleId){
            return ResponseResult
                    .ok()
                    .data("isCollect",collectionService.isCollect(userId, articleId));
    }

    @GetMapping("/addOrDeleteCollection/{userId}/{articleId}/{isAdd}")
    public ResponseResult addOrDeleteCollection(@PathVariable Integer articleId,
                                                @PathVariable String userId,
                                                @PathVariable String isAdd){
        boolean add = Boolean.parseBoolean(isAdd);
        if(collectionService.addOrDeleteCollect(add, userId, articleId))
            if(add)
        return ResponseResult.ok().data("isAdd","add");
            else
        return ResponseResult.ok().data("isAdd","delete");
            return ResponseResult.error().message("操作失败");
    }
@RequestMapping("/getArticleCollections/{userId}/{pageSize}/{limit}")
    public ResponseResult getCollections(@PathVariable Integer limit,
                                         @PathVariable Integer pageSize,
                                         @PathVariable String userId){
    Map<String, Object> collection = collectionService.getArticleCollection(userId, pageSize, limit);
    return ResponseResult.ok().data(collection);
}
}

